package com.hotel.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.common.Result;
import com.hotel.dto.RoomDTO;
import com.hotel.dto.RoomTypeDTO;
import com.hotel.entity.Room;
import com.hotel.service.RoomService;
import com.hotel.service.RoomTypeService;
import com.hotel.vo.RoomTypeVO;
import com.hotel.vo.RoomVO;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 房间管理控制器
 * 
 * @author Hotel Management Team
 */
@RestController
@RequestMapping("/room")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class RoomController {

    private final RoomService roomService;

    private final RoomTypeService roomTypeService;

    /**
     * 获取房间列表
     */
    @GetMapping("/list")
    public Page<RoomVO> getRoomList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Long roomTypeId,
            @RequestParam(required = false) Integer floor,
            @RequestParam(required = false) String keyword) {
        Page<RoomVO> rooms = roomService.getRoomPage(page, size, keyword ,status, floor);
        return rooms;
    }

    /**
     * 获取房间详情
     */
    @GetMapping("/{id}")
    public Result<RoomVO> getRoomById(@PathVariable Long id) {
        RoomVO room = roomService.getRoomById(id);
        return Result.success(room);
    }

    /**
     * 根据房间号获取房间
     */
    @GetMapping("/number/{roomNumber}")
    public Result<Room> getRoomByNumber(@PathVariable String roomNumber) {
        Room room = roomService.getRoomByNumber(roomNumber);
        return Result.success(room);
    }

    /**
     * 创建房间
     */
    @PostMapping
    public Result<Void> createRoom(@Valid @RequestBody RoomDTO room) {
        roomService.createRoom(room);
        return Result.success("房间创建成功");
    }

    /**
     * 更新房间信息
     */
    @PutMapping("/{id}")
    public Result<Void> updateRoom(@PathVariable Long id, @Valid @RequestBody RoomDTO room) {
        //room.setId(id);
        roomService.updateRoom(id,room);
        return Result.success("房间更新成功");
    }

    /**
     * 删除房间
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteRoom(@PathVariable Long id) {
        roomService.deleteRoom(id);
        return Result.success("房间删除成功");
    }

    /**
     * 更新房间状态
     */
    @PostMapping("/{id}/status")
    public Result<Void> updateRoomStatus(@PathVariable Long id, @Valid @RequestBody Map<String, Object> request) {
        String status = (String) request.get("status");
        roomService.updateRoomStatus(id, status);
        return Result.success("房间状态更新成功");
    }

    /**
     * 批量更新房间状态
     */
    @PostMapping("/batch-status")
    public Result<Void> batchUpdateRoomStatus(@RequestBody Map<String, Object> request) {
        @SuppressWarnings("unchecked")
        List<Long> roomIds = (List<Long>) request.get("roomIds");
        String status = (String) request.get("status");
        String remarks = (String) request.get("remarks");
        roomService.batchUpdateRoomStatus(roomIds, status);
        return Result.success("批量更新成功");
    }

    /**
     * 获取可用房间列表
     */
    @GetMapping("/available")
    public Result<List<RoomVO>> getAvailableRooms(
            @RequestParam(required = false) Long roomTypeId,
            @RequestParam(required = false) String checkinDate,
            @RequestParam(required = false) String checkoutDate) {
        List<RoomVO> rooms = roomService.getAvailableRooms(roomTypeId, checkinDate, checkoutDate);
        return Result.success(rooms);
    }

    /**
     * 获取房间状态统计
     */
    @GetMapping("/status-stats")
    public Result<Map<String, Object>> getRoomStatusStats() {
        Map<String, Object> stats = roomService.getRoomStatusStats();
        return Result.success(stats);
    }

    /**
     * 获取楼层房间分布
     */
    @GetMapping("/floor-distribution")
    public Result<List<Map<String, Object>>> getFloorDistribution() {
        List<Map<String, Object>> distribution = roomService.getFloorRoomDistribution();
        return Result.success(distribution);
    }

    /**
     * 房间清洁完成
     */
    @PostMapping("/{id}/clean-complete")
    public Result<Void> markCleanComplete(@PathVariable Long id) {
        roomService.markRoomCleaned(id);
        return Result.success("房间清洁完成");
    }

    /**
     * 房间维护完成
     */
    @PostMapping("/{id}/maintenance-complete")
    public Result<Void> markMaintenanceComplete(@PathVariable Long id) {
        roomService.markRoomMaintained(id);
        return Result.success("房间维护完成");
    }

    // ==================== 房型管理 ====================

    /**
     * 获取房型列表
     */
    @GetMapping("/type/list")
    public Result<List<RoomTypeVO>> getRoomTypeList() {
        List<RoomTypeVO> roomTypes = roomTypeService.getEnabledRoomTypes();
        return Result.success(roomTypes);
    }

    /**
     * 获取房型详情
     */
    @GetMapping("/type/{id}")
    public Result<RoomTypeVO> getRoomTypeById(@PathVariable Long id) {
        RoomTypeVO roomType = roomTypeService.getRoomTypeById(id);
        return Result.success(roomType);
    }

    /**
     * 创建房型
     */
    @PostMapping("/type")
    public Result<Void> createRoomType(@Valid @RequestBody RoomTypeDTO roomType) {
        roomTypeService.createRoomType(roomType);
        return Result.success("房型创建成功");
    }

    /**
     * 更新房型
     */
    @PutMapping("/type/{id}")
    public Result<Void> updateRoomType(@PathVariable Long id, @Valid @RequestBody RoomTypeDTO roomType) {

        roomTypeService.updateRoomType(id,roomType);
        return Result.success("房型更新成功");
    }

    /**
     * 删除房型
     */
    @DeleteMapping("/type/{id}")
    public Result<Void> deleteRoomType(@PathVariable Long id) {
        roomTypeService.deleteRoomType(id);
        return Result.success("房型删除成功");
    }

}